package com.shadow.disnet.disrutor;


import com.shadow.disnet.netty.ILog;

import java.util.UUID;

/**
 * 事件对象
 * 事件包含的属性有哪些呢？
 */
public class ShadowEvent<T> implements Event<T>, ILog {
    private String id;
    private MessageProducer<T> messageProducer;
    private int moduleID;
    private int msgId;
    private T msg;
    private Runnable task;

    public ShadowEvent() {
        this.id = UUID.randomUUID().toString().replace("-", "");
    }


    /**
     * 设置事件
     */
    public void setEvent(int moduleID, int msgId, T msg, MessageProducer messageProducer) {
        this.messageProducer = messageProducer;
        this.moduleID = moduleID;
        this.msgId = msgId;
        this.msg = msg;
    }

    @Override
    public void handle() {
        if (task != null) { //有任务先执行
            task.run();
       info("有任务先执行");
        } else { //否则执行用户线程
            messageProducer.onMessage(moduleID, msgId, msg);
            info("moduleID=%s,msgId=%s,msg=%s", moduleID, msgId, msg);
        }
    }

    @Override
    public String id() {
        return id;
    }

    @Override
    public void setTask(Runnable task) {
        this.task = task;
    }

}
